package com.xiaoyu.base.utils

import android.content.Context
import androidx.annotation.StringDef
import com.tencent.mmkv.MMKV
import com.xiaoyu.base.data.UserData

class MMKVUtil {

    companion object {
        const val MultiProcessKV = "MultiProcessKV"

        @StringDef(MultiProcessKV)
        annotation class MMKVIdDef

        @JvmStatic
        fun init(context: Context) {
            MMKV.initialize(context)
        }

        @JvmStatic
        fun getDefault(): MMKV {
            return MMKV.defaultMMKV();
        }

        @JvmStatic
        fun get(): MMKV? {
            if (UserData.getInstance().hasLogin()) {
                return get(UserData.getInstance().uid)
            }
            return null
        }

        @JvmStatic
        fun get(@MMKVIdDef id: String): MMKV {
            return MMKV.mmkvWithID(id)
        }

        @JvmStatic
        fun getMultiProcessKV(@MMKVIdDef id: String): MMKV {
            return MMKV.mmkvWithID(id, MMKV.MULTI_PROCESS_MODE)
        }

        @JvmStatic
        fun getMultiProcessKV(): MMKV? {
            if (UserData.getInstance().hasLogin()) {
                return getMultiProcessKV(MultiProcessKV + "_" + UserData.getInstance().uid)
            }
            return null
        }
    }
}